package com.hyphenate.easeui.utils;

import android.os.Environment;

import com.hyphenate.chat.EMMessage;
import com.zhong.bmob.model.ChatMessageBase;
import com.zhong.bmob.model.FileSavePath;

import java.io.File;
import java.io.IOException;
import java.util.List;

import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.DownloadFileListener;

/**
 * Created by 俊峰 on 2017/5/25.
 */

public class OtherFileDownLoadFromBmob {
    public static void doOtherFileDownLoad(final List<EMMessage> messages, String fileName, final int type, String url, IOtherFileDownLoadFromBmob otherFileDownLoadFromBmob, final ChatMessageBase base){
        final IOtherFileDownLoadFromBmob loadFromBmob = otherFileDownLoadFromBmob;
        String theFileName = null;
        if (type == 1){
            theFileName = fileName + ".jpg";
        }else if (type == 2){
            theFileName = fileName + ".amr";
        }
        BmobFile bmobFile = new BmobFile(theFileName, "", url);
        File file = new File(Environment.getExternalStorageDirectory(), FileSavePath.theChatHistory);
        if (!file.exists()) {
            if (file.mkdir()) {
                File chatFile = new File(file, theFileName);
                if (!chatFile.exists()) {
                    try {
                        if (chatFile.createNewFile()) {
                            bmobFile.download(chatFile, new DownloadFileListener() {
                                @Override
                                public void done(String s, BmobException e) {
                                    if (e == null){
                                        loadFromBmob.OtherFileDownLoadSuccess(messages, s, type, base);
                                    }else {
                                        loadFromBmob.OtherFileDownLoadFail(e);
                                    }
                                }

                                @Override
                                public void onProgress(Integer integer, long l) {

                                }
                            });
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }else {
            File chatFile = new File(file, theFileName);
            if (!chatFile.exists()) {
                try {
                    if (chatFile.createNewFile()) {
                        bmobFile.download(chatFile, new DownloadFileListener() {
                            @Override
                            public void done(String s, BmobException e) {
                                if (e == null){
                                    loadFromBmob.OtherFileDownLoadSuccess(messages, s, type, base);
                                }else {
                                    loadFromBmob.OtherFileDownLoadFail(e);
                                }
                            }

                            @Override
                            public void onProgress(Integer integer, long l) {

                            }
                        });
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
